####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Table A8
###
####################################################################################


library(miceadds)


################################################################################
# Set Up
################################################################################


# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
source(paste0(wd_code, '/01_data_load_clean.R'))


################################################################################
# Functions
################################################################################


cleantable = function(container){
  colnames(container)=c('coef', 'se', 'tval', 'pval', 'obs', 'mean', 'model', 'tercile')
  container[,1:6] = sapply(container[,1:6], as.numeric)
  container[,1:6] = round(container[,1:6], 3)
  container$se = paste0('(',  as.character(container$se), ')')
  container$coef = ifelse(container$pval<0.01, paste0(as.character(container$coef), '***'), 
                          ifelse(container$pval>0.01 & container$pval<0.05, paste0(as.character(container$coef), '**'),
                                 ifelse(container$pval>0.05 & container$pval<0.1, paste0(as.character(container$coef), '*'), as.character(container$coef))))
  container = t(container)
  colnames(container) = container['model',]
  return(container)
}


################################################################################
# Estimation
################################################################################

container = data.frame()
for (n in c(0, 1,2,3)){
  print(paste0('--------------Immigation tercile ', n, ' -------------- '))
  
  df1 = df_did[which(!is.na(df_did$fn) & !is.na(df_did$imm_share_90) & !is.na(df_did$hlm_share_90)),]
  if (n>0){df1 = df1[which(df1$imm_quant_99==n),]}
  
  # * Extreme Left *
  temp = lm.cluster(data=df1, extreme.left ~  postT + running + factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  container = rbind(container, c(summary(temp)[2, ], nrow(df1[!is.na(df1$extreme.left),]), mean(df1$extreme.left, na.rm = T ), 'extr left', n))
  
  # * Left *
  temp = lm.cluster(data=df1, left ~  postT + running + factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  container = rbind(container, c(summary(temp)[2, ], nrow(df1[!is.na(df1$left),]), mean(df1$left, na.rm = T ), 'left', n))
  
  # * Right *
  temp = lm.cluster(data=df1, right ~  postT + running + factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  container = rbind(container, c(summary(temp)[2, ], nrow(df1[!is.na(df1$right),]), mean(df1$right, na.rm = T ), 'right', n))
  
  # * FN *
  temp = lm.cluster(data=df1, fn ~  postT + running + factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  container = rbind(container, c(summary(temp)[2, ], nrow(df1[!is.na(df1$fn),]), mean(df1$fn, na.rm = T ) , 'fn', n))
  
  # * turnout *
  temp = lm.cluster(data=df1, turnout ~  postT + running + factor(CODGEO) + factor(policy_period), cluster = df1$CODGEO)
  container = rbind(container, c(summary(temp)[2, ], nrow(df1[!is.na(df1$turnout),]), mean(df1$turnout, na.rm = T ), 'turnout', n))
  
}


output = cleantable(container)
print(output)